package com.cskaoyan.service.wx;

import com.cskaoyan.bean.Address;
import com.cskaoyan.bean.AddressExample;
import com.cskaoyan.bean.Region;
import com.cskaoyan.bean.RegionExample;
import com.cskaoyan.bean.wx.AddressEditVo;
import com.cskaoyan.mapper.AddressMapper;
import com.cskaoyan.mapper.RegionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AddressServiceImpl implements AddressService {

    @Autowired
    AddressMapper addressMapper;
    @Autowired
    RegionMapper regionMapper;

    @Override
    public List<Address> addressList(Integer userId) {

        AddressExample addressExample = new AddressExample();
        AddressExample.Criteria criteria = addressExample.createCriteria();
        criteria.andUserIdEqualTo(userId);
        List<Address> addressList = addressMapper.selectByExample(addressExample);
        return  addressList;

    }

    @Override
    public List<Region> regionList(Integer pid) {
        RegionExample regionExample = new RegionExample();
        RegionExample.Criteria criteria = regionExample.createCriteria();
        criteria.andPidEqualTo(pid);
        List<Region> regions = regionMapper.selectByExample(regionExample);
        return regions;
    }

    @Override
    public void addressSave(Address address) {
        if (address.getIsDefault() == true) {
            AddressExample addressExample = new AddressExample();
            AddressExample.Criteria criteria = addressExample.createCriteria();
            criteria.andUserIdEqualTo(address.getUserId());
            criteria.andIsDefaultEqualTo(true);
            Address addressUpdateBySelective = new Address();
            addressUpdateBySelective.setIsDefault(false);
            addressMapper.updateByExampleSelective(addressUpdateBySelective,addressExample);
        }
        if (address.getId() != 0){
            AddressExample addressExample = new AddressExample();
            AddressExample.Criteria criteria = addressExample.createCriteria();
            criteria.andIdEqualTo(address.getId());
            addressMapper.updateByExampleSelective(address, addressExample);
        }
        if (address.getId() == 0){
            addressMapper.insert(address);
        }

    }

    @Override
    public AddressEditVo addressDetail(Integer id) {
        Address address = addressMapper.selectByPrimaryKey(id);
        Region regionForProvinceName = regionMapper.selectByPrimaryKey(address.getProvinceId());
        Region regionForCityName = regionMapper.selectByPrimaryKey(address.getCityId());
        Region regionForAreaName = regionMapper.selectByPrimaryKey(address.getAreaId());
        AddressEditVo addressEditVo = new AddressEditVo(address.getIsDefault(), address.getAreaId(),
                address.getAddress(),regionForCityName.getName(),regionForAreaName.getName(), address.getName(),
                address.getMobile(),address.getId(),address.getCityId(),regionForProvinceName.getName(),
                address.getProvinceId());
        return addressEditVo;
    }

    @Override
    public void addressDelete(Integer id) {
        addressMapper.deleteByPrimaryKey(id);
    }
}
